package com.controller;

import com.entity.UserInfo;
import com.service.UserInfoService;
import com.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/api")
public class UserInfoController {
	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 登录
	 *
	 * @param username
	 * @param password
	 * @return
	 */
	@GetMapping("/userInfo/login")
	public ResponseVO getAll(String username, String password) {
		ResponseVO responseVO = ResponseVO.generateResponseVO();
		UserInfo userInfo = userInfoService.getByUsername(username);
		if (userInfo != null && password.equals(userInfo.getPassWord())) {
			responseVO.setCode("200");
			responseVO.setMsg("登录成功");
			responseVO.setData(userInfo);
		} else {
			responseVO.setCode("500");
			responseVO.setMsg("账号或密码错误");
		}
		return responseVO;
	}
	
	@GetMapping("/userInfo")
	public ResponseVO getAll() {
		ResponseVO responseVO = new ResponseVO();
		responseVO.setData(userInfoService.getAll());
		return responseVO;
	}
	
	/**
	 * 根据用户账号获取用户信息
	 *
	 * @param userName 用户账号
	 * @return
	 */
	@GetMapping("/userInfo/getByUsername")
	public ResponseVO getByIdUserInfo(String userName) {
		return new ResponseVO("200",null,userInfoService.getByUsername(userName));
	}
	
	/**
	 * 修改用户信息
	 * 修改内容为：昵称、性别、头像、电子邮箱、手机号码、地址、个人描述
	 * 根据账号或ID修改
	 *
	 * @param userInfo 用户信息
	 * @return
	 */
	@PostMapping("/userInfo/edit")
	public ResponseVO editUserInfo(@RequestBody UserInfo userInfo) {
		return new ResponseVO("200",null,userInfoService.editUserInfo(userInfo));
	}
}
